package com.zhaotianhao.serviceproduct.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaotianhao.serviceproduct.entity.Sku;
import com.zhaotianhao.serviceproduct.entity.SpecParam;
import com.zhaotianhao.serviceproduct.mapper.SkuMapper;
import com.zhaotianhao.serviceproduct.service.ISkuService;
import com.zhaotianhao.serviceproduct.service.ISpecParamService;
import com.zhaotianhao.serviceproduct.service.ISpuService;
import com.zhaotianhao.serviceproduct.vo.SkuDetailVO;
import com.zhaotianhao.serviceproduct.vo.SkuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * sku表,该表表示具体的商品实体,如黑色的 64g的iphone 8 服务实现类
 * </p>
 *
 * @author Z-7
 * @since 2022-04-27
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements ISkuService {

    @Autowired
    private ISpuService iSpuService;
    @Autowired
    private ISpecParamService iSpecParamService;
    /**
     * 我的购物车->商品详情
     *
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuVo> getCartSkuInfoByIds(List<Long> skuIds) {
        List<SkuVo> skuVos = new ArrayList<>();
        skuIds.forEach(skuId->{
            SkuDetailVO skuDetailVO = iSpuService.getSkuDetailVO(skuId);

            List<SpecParam> specParams = iSpecParamService.queryAllSpecParam();

            /*------------------设置私有属性-------------------*/
            // 将sku json转为map
            LinkedHashMap linkedHashMap = JSONObject.parseObject(getById(skuId).getOwnSpec(), LinkedHashMap.class);
            skuDetailVO.setGroupVOS(iSpuService.setGroupVOS(specParams, linkedHashMap));
            SkuVo skuVo = new SkuVo();
            BeanUtils.copyProperties(skuDetailVO,skuVo);
            skuVos.add(skuVo);
        });
        return skuVos;
    }
}
